<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Auction extends CI_Model {
    public function __construct() {
    	parent::__construct();
    	$this->load->model('pageauction');
        $this->load->model('news');
        $this->load->model('page');
    }
	    
    public function getAuctions($options = array()) {   
    	$options = _default(array('sortDirection' => 'desc', 'sortBy' => 'auc_enddate'), $options);
    	$queryFields = array('auc_id', 'auc_startdate', 'auc_enddate', 
                             'auc_description', 'auc_body', 'auc_article', 
                             'auc_contact', 'auc_enddate >', 'auc_enddate >=',
                             'auc_enddate <', 'auc_enddate <=', 'auc_page',
                             'auc_detailpage', 'auc_type');

    	foreach ($queryFields as $field) {
    		if (isset($options[$field]))
    			$this->db->where($field, $options[$field]);
    	}
    	
    	if (isset($options['limit']) && isset($options['offset']))
    		$this->db->limit($options['limit'], $options['offset']);
    	else if (isset($options['limit']))
    		$this->db->limit($options['limit']);
    	
    	if (isset($options['sortBy']))
    		$this->db->order_by($options['sortBy'], $options['sortDirection']);
    	$query = $this->db->get('acs_auctions');
    	if ($query->num_rows() == 0)
    		return FALSE;
    	if (isset($options['auc_id']) || isset($options['auc_detailpage']))
    		return $query->row_array(0);
    	else
    		return $query->result_array();     
    }
    
    public function countAuctions($options = array()) {   
    	$queryFields = array('auc_id', 'auc_startdate', 'auc_enddate', 
                             'auc_description', 'auc_body', 'auc_article', 
                             'auc_contact', 'auc_enddate >', 'auc_enddate >=',
                             'auc_enddate <', 'auc_enddate <=', 'auc_page', 'auc_detailpage');

    	foreach ($queryFields as $field) {
    		if (isset($options[$field]))
    			$this->db->where($field, $options[$field]);
    	}
    	
    	if (isset($options['limit']) && isset($options['offset']))
    		$this->db->limit($options['limit'], $options['offset']);
    	else if (isset($options['limit']))
    		$this->db->limit($options['limit']);
    	
        $this->db->from('acs_auctions');
    	$count = $this->db->count_all_results();
    	return $count;   
    }
    
    public function getLast10($options = array())
    {
        $opts = _default(array('sortBy' => 'auc_startdate', 'sortDirection' => 'desc', 'limit' => 10), $options);
        return $this->getAuctions($opts);
    }
    
    public function updateAuction($options = array()) {
		$fields = array('auc_name', 'auc_description', 'auc_startdate', 'auc_enddate', 
		                'auc_contact', 'auc_owner', 'auc_contact',
		                'auc_logopath', 'auc_address', 'auc_startprice',
		                'auc_type', 'auc_detailpage');
		if (!_required(array('auc_id'), $options)) {
			return FALSE;
		}
		foreach ($fields as $field) {
			if (isset($options[$field]))
				$this->db->set($field, $options[$field]);
		}
        $this->db->where('auc_id', $options['auc_id']);
		$this->db->update('acs_auctions');
		return $this->db->affected_rows();
    }
    
    public function insertAuction($options = array()) {
		$aucid = FALSE;
		$pagid = FALSE;
		$fields = array('auc_name', 'auc_description', 'auc_enddate', 
		                'auc_contact', 'auc_owner', 'auc_contact',
		                'auc_logopath', 'auc_address', 'doc_startprice',
		                'auc_type');
		if (!_required(array('auc_name', 'auc_description', 'auc_startdate', 'auc_startprice', 'auc_type'),
		               $options)) {
			return FALSE;
		}
		foreach ($fields as $field) {
			if (isset($options[$field]))
				$this->db->set($field, $options[$field]);
		}
        $this->db->insert('acs_auctions');
		$aucid = $this->db->insert_id();
		$pagid = $this->page->insertPage(array(
		     'pag_title' => 'Licitatie publica: ' . $options['auc_name'],
		     'pag_name' => str_replace(' ', '_', $options['auc_name']),
		     'pag_description' => $options['auc_description']));
		if ($aucid != FALSE && $pagid != FALSE) {
            $this->news->insertNews(array(
		     'new_title' => 'Licitatie publica: ' . $options['auc_name'],
		     'new_type' => 'L',
		     'new_objid' => $aucid,
             'new_route' => 'site/pages/' . str_replace(' ', '_', $options['auc_name'])));
            $this->updateAuction(array('auc_id' => $aucid, 'auc_detailpage' => $pagid));
			$this->pageauction->addAuctionToPage(array(
												'ap_auction' => $aucid,
												'ap_page' => $pagid));
			return $aucid;
        } else {
			return FALSE;
		}
		
    }
    
    public function deleteAuction($options = array()) {
		return TRUE;    	
    }
}
